﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.UI.HtmlControls;
using V5_Common;
using V5_Model;
using V5_BLL;

namespace V5_EShop.Controls
{
    public partial class getShoppingCart : BasicControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //检查用户登录
            if (!this.IsPostBack)
            {
                int proid = Input.SetInt(Request.QueryString["pid"]);
                string action = Input.SetString(Request.Params["act"]);

                switch (action.Trim().ToLower())
                {
                    case "edit":
                        Bind_CartEdit(proid);
                        break;
                    case "del":
                        Bind_CartDel(proid);
                        break;
                    default:
                        Bind_AddToCart(proid);
                        break;
                }
            }
        }

        ////添加到购物车
        private void Bind_AddToCart(int pid)
        {
            if (pid < 1)
            {
                return;
            }
            DataTable dtProducts = null;
            if (Session["dtProducts"] == null)
            {
                dtProducts = createProductDT();
                //开始添加购物
                ModelProduct model = new BLLProduct().GetModel(pid, true);
                DataRow aProduct = dtProducts.NewRow();
                aProduct["proid"] = pid;
                aProduct["prototalprice"] = (Input.SetInt(model.ProductMarketPrice) * 1);//市场价
                aProduct["proname"] = model.ProductName;
                aProduct["proprice"] = model.ProductMarketPrice;//产品价格
                aProduct["quantity"] = 1;//产品数量
                dtProducts.Rows.Add(aProduct);
                this.gvProducts.DataSource = dtProducts;
                this.gvProducts.DataBind();
                Session["dtProducts"] = dtProducts;
            }
            else
            {
                bool found = false;
                dtProducts = (DataTable)Session["dtProducts"];
                //循环购物车 是否有重复的产品
                for (int i = 0; i < dtProducts.Rows.Count; i++)
                {
                    if (Convert.ToInt32(dtProducts.Rows[i]["proid"]) == pid)
                    {
                        found = true;
                        break;
                    }
                }
                if (!found)
                {
                    ModelProduct model = new BLLProduct().GetModel(pid, true);
                    DataRow aProduct = dtProducts.NewRow();
                    aProduct["proid"] = pid;
                    aProduct["prototalprice"] = (Input.SetInt(model.ProductMarketPrice) * 1);//市场价
                    aProduct["proname"] = model.ProductName;
                    aProduct["proprice"] = model.ProductMarketPrice;//产品价格
                    aProduct["quantity"] = 1;//产品数量
                    dtProducts.Rows.Add(aProduct);
                }
                Session["dtProducts"] = dtProducts;

                this.gvProducts.DataSource = dtProducts;
                this.gvProducts.DataBind();
            }
        }

        //建立购物车
        private DataTable createProductDT()
        {
            DataTable dtProducts = new DataTable();
            //产品ID
            DataColumn productColumn = new DataColumn();
            productColumn.DataType = System.Type.GetType("System.Int32");
            productColumn.ColumnName = "proid";
            productColumn.Unique = true;
            dtProducts.Columns.Add(productColumn);
            //产品总价
            productColumn = new DataColumn();
            productColumn.DataType = System.Type.GetType("System.Int32");
            productColumn.ColumnName = "prototalprice";
            dtProducts.Columns.Add(productColumn);
            //产品名称
            productColumn = new DataColumn();
            productColumn.DataType = System.Type.GetType("System.String");
            productColumn.ColumnName = "proname";
            dtProducts.Columns.Add(productColumn);
            //产品价格
            productColumn = new DataColumn();
            productColumn.DataType = System.Type.GetType("System.Double");
            productColumn.ColumnName = "proprice";
            dtProducts.Columns.Add(productColumn);
            //产品数量
            productColumn = new DataColumn();
            productColumn.DataType = System.Type.GetType("System.Int32");
            productColumn.ColumnName = "quantity";
            dtProducts.Columns.Add(productColumn);
            //使"id"成为主键
            DataColumn[] pkColumns = new DataColumn[1];
            pkColumns[0] = dtProducts.Columns["proid"];
            dtProducts.PrimaryKey = pkColumns;
            return dtProducts;
        }

        //编辑
        private void Bind_CartEdit(int pid)
        {
            foreach (GridViewRow item in this.gvProducts.Rows)
            {
                TextBox tb = (TextBox)item.FindControl("txtquantity");
                int procount = Input.SetInt(tb.Text);
                int keyid = Input.SetInt(this.gvProducts.DataKeys[item.RowIndex].Value);
                if (keyid == pid)
                {
                    DataTable dtProducts = (DataTable)Session["dtProducts"];
                    DataRow[] DataRows = dtProducts.Select(" proid=" + pid);
                    foreach (DataRow item1 in DataRows)
                    {
                        item1["quantity"] = procount;
                        item1["prototalprice"] = procount * Input.SetInt(item1["proprice"]);
                        item1.EndEdit();
                    }
                    dtProducts.AcceptChanges();
                    this.gvProducts.DataSource = dtProducts;
                    this.gvProducts.DataBind();
                    Session["dtProducts"] = dtProducts;
                }
            }
            Response.Redirect(HTTPS.ComeUrl);
        }
        //删除
        private void Bind_CartDel(int pid)
        {
            DataTable dtProducts = (DataTable)Session["dtProducts"];
            DataRow[] drArr = dtProducts.Select(" proid=" + pid);
            dtProducts.Rows.Remove(drArr[0]);
            dtProducts.AcceptChanges();
            this.gvProducts.DataSource = dtProducts;
            this.gvProducts.DataBind();
            Session["dtProducts"] = dtProducts;
            Response.Redirect(HTTPS.ComeUrl);
        }

        protected void gvProducts_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string eName = Input.SetString(e.CommandName);
            int id = Input.SetInt(e.CommandArgument);
            switch (eName.ToLower().Trim())
            {
                case "mmedit":
                    Bind_CartEdit(id);
                    break;
                case "mmdel":
                    Bind_CartDel(id);
                    break;
                default:
                    break;
            }
        }
    }
}